package eteam.aps.service.impl;


import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.OnlyProductMapper;
import eteam.aps.model.entity.OnlyProduct;
import eteam.aps.model.entity.ProductParentEntity;
import eteam.aps.model.vo.OnlyProductVo;
import eteam.aps.service.OnlyProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 唯一产品业务层
 *
 * @author WMT
 * @Date 2023/2/9 15:55
 */
@Service
public class OnlyProductServiceIml implements OnlyProductService {

    @Autowired
    private OnlyProductMapper onlyProductMapper;

    @Transactional
    @Override
    public int insert(OnlyProduct onlyProduct) {
        return onlyProductMapper.insert(onlyProduct);
    }

    @Transactional
    @Override
    public int setFreeze(OnlyProduct onlyProduct) {
        return onlyProductMapper.setFreeze(onlyProduct);
    }


    @Override
    public OnlyProduct selectByCode(String code) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("code",code);
        map.put("limit", ProductParentEntity.IS_SERIAL_ENABLE);
        return onlyProductMapper.selectByCode(map);
    }

    @Override
    public JsonPage<OnlyProductVo> selectAll(Map<String, Object> map, int page, int pageSize, int productCategory) {
        map.put("productCategory",productCategory);
        map.put("fState", ProductParentEntity.FSTATE_FREEZE);
        List<OnlyProductVo> list = null;
        int totalCount = onlyProductMapper.selectCountAll(map);
        if (totalCount > 0){
            map.put("offset", (page - 1) * pageSize);
            map.put("limit", pageSize);
            list = onlyProductMapper.selectAll(map);
        }
        return JsonPage.builder(page,pageSize,totalCount,list);
    }

}
